package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;
import java.util.List;

/**
 * @author ZZQ
 * @PackageName: com.neutech.maplestory.entity
 * @ClassName: Item
 * @Description:
 * @date 2021/1/29 16:04
 */
public class Item extends AbstractMapleStoryObject{
    public Item(){}

    /**
     * 道具类型
     */
    public int type;
    public Image img;

    public boolean jump;
    /**
     * 当前道具的数量
     */
    public int qty = 1;

    public Item(MapleStoryClient msc,int x, int y,int type){
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.jump = true;
        this.live = true;
        this.type = type;
        switch (type){
            case 0:
                this.img = ImageUtil.getImage("HP_50");
                break;
            case 1:
                this.img = ImageUtil.getImage("HP_100");
                break;
            case 2:
                this.img = ImageUtil.getImage("HP_300");
                break;
            case 3:
                this.img = ImageUtil.getImage("MP_100");
                break;
            default:
                break;
        }
        this.width = 25;
        this.height = 25;
    }
    @Override
    public void draw(Graphics g) {
        if(!live){
            msc.items.remove(this);
            return;
        }
        g.drawImage(img,x,y,null);
        move();
    }

    @Override
    public void move() {
        /*if (jump){
            jump();
        }*/
        if (jump) {
            jump(msc.grounds);
        }else {
            //自由落体（往下掉）
            jumpdown(msc.grounds);
        }
    }


//        // vt - v0 + gt 竖直上抛
        private double v0 = Constant.INIT_ITEM_SPEED;
        private double vt;
        private double g = 9.8;
        private double delta_height;
        private double t = 0.7;
        /**
         * 跳跃的方法
         */
        /*private void jump() {
//        // vt - v0 + gt 竖直上抛
            vt = v0 - g * t;
            delta_height = v0 * t;
            v0 = vt;
            y -= delta_height;
            // 自由落体的结束位置
            if(y >= 553){
                jump = false;
                v0 = Constant.INIT_ITEM_SPEED;
                vt = 0.0;
                y = 553;
            }
        }*/

        public void jump(List<Ground> grounds) {
            if (!drop) {
                vt = v0 - g * t;
                delta_height = v0 * t;
                v0 = vt;
                y -= delta_height;
                //什么时候往下掉
                if (vt <= 0) {
                    vt = 0;
                    drop = true;
                }
            } else {
                jumpdown(grounds);
            }
        }
    private boolean drop = true;

    public void jumpdown(List<Ground>grounds){
        //掉的过程（自由落体）
        vt = v0 + g * t;
        delta_height = v0 * t;
        v0 = vt;
        y += delta_height;
        //什么时候停止自由落体
        for (int i = 0;i < grounds.size();i ++){
            Ground ground = grounds.get(i);
            if(this.getRectangle().intersects(ground.getRectangle()) && this.y <= ground.y - this.height + 40){
                //加40 是40ms的距离不做碰撞检测 可掉出地图
                //停了

                if (this.y >= ground.y - this.height){
                    this.y = ground.y - this.height;
                    drop = false;
                    jump = false;
                    v0 = Constant.INIT_JUMP_SPEED;
                    vt = 0.0;
                    break;
                }
            }else {
                drop = true;
            }
        }

    }
}


